#include "settings_dialog.h"
#include <QApplication>

SettingsDialog::SettingsDialog(QWidget *parent)
    : QDialog(parent)
    , m_mainLayout(nullptr)
    , m_layoutGroup(nullptr)
    , m_circularRadio(nullptr)
    , m_scatteredRadio(nullptr)
    , m_layoutButtonGroup(nullptr)
    , m_okButton(nullptr)
    , m_cancelButton(nullptr)
    , m_currentLayout(CircularLayout)
    , m_settings(nullptr)
{
    setupUI();
    loadSettings();
}

SettingsDialog::~SettingsDialog()
{
    if (m_settings) {
        delete m_settings;
    }
}

void SettingsDialog::setupUI()
{
    setWindowTitle("菜单设置");
    setFixedSize(350, 200);
    setWindowFlags(Qt::Dialog | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
    
    // 应用现代化样式
    setStyleSheet(
        "QDialog {"
        "    background-color: rgb(250, 250, 250);"
        "    border-radius: 8px;"
        "}"
        "QGroupBox {"
        "    font-weight: bold;"
        "    border: 2px solid rgb(200, 200, 200);"
        "    border-radius: 5px;"
        "    margin-top: 10px;"
        "    padding-top: 10px;"
        "}"
        "QGroupBox::title {"
        "    subcontrol-origin: margin;"
        "    left: 10px;"
        "    padding: 0 5px 0 5px;"
        "}"
        "QRadioButton {"
        "    font-size: 12px;"
        "    spacing: 8px;"
        "    padding: 5px;"
        "}"
        "QRadioButton::indicator {"
        "    width: 16px;"
        "    height: 16px;"
        "}"
        "QPushButton {"
        "    background-color: rgb(70, 130, 250);"
        "    color: white;"
        "    border: none;"
        "    border-radius: 6px;"
        "    padding: 8px 16px;"
        "    font-weight: bold;"
        "}"
        "QPushButton:hover {"
        "    background-color: rgb(50, 110, 230);"
        "}"
        "QPushButton:pressed {"
        "    background-color: rgb(40, 100, 220);"
        "}"
        "QPushButton#cancelButton {"
        "    background-color: rgb(150, 150, 150);"
        "}"
        "QPushButton#cancelButton:hover {"
        "    background-color: rgb(130, 130, 130);"
        "}"
    );
    
    // 创建主布局
    m_mainLayout = new QVBoxLayout(this);
    m_mainLayout->setSpacing(15);
    m_mainLayout->setContentsMargins(20, 20, 20, 20);
    
    // 布局选择组
    m_layoutGroup = new QGroupBox("菜单布局", this);
    QVBoxLayout *layoutGroupLayout = new QVBoxLayout(m_layoutGroup);
    
    m_circularRadio = new QRadioButton("圆形布局 - 菜单项围绕中心按钮排列", m_layoutGroup);
    m_scatteredRadio = new QRadioButton("分散布局 - 菜单项随机分散排列", m_layoutGroup);
    
    // 创建按钮组来管理单选按钮
    m_layoutButtonGroup = new QButtonGroup(this);
    m_layoutButtonGroup->addButton(m_circularRadio, CircularLayout);
    m_layoutButtonGroup->addButton(m_scatteredRadio, ScatteredLayout);
    
    layoutGroupLayout->addWidget(m_circularRadio);
    layoutGroupLayout->addWidget(m_scatteredRadio);
    
    // 按钮布局
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->addStretch();
    
    m_cancelButton = new QPushButton("取消", this);
    m_cancelButton->setObjectName("cancelButton");
    m_okButton = new QPushButton("确定", this);
    
    buttonLayout->addWidget(m_cancelButton);
    buttonLayout->addWidget(m_okButton);
    
    // 添加到主布局
    m_mainLayout->addWidget(m_layoutGroup);
    m_mainLayout->addStretch();
    m_mainLayout->addLayout(buttonLayout);
    
    // 连接信号
    connect(m_okButton, &QPushButton::clicked, this, &SettingsDialog::onOkClicked);
    connect(m_cancelButton, &QPushButton::clicked, this, &SettingsDialog::onCancelClicked);
    connect(m_layoutButtonGroup, QOverload<int>::of(&QButtonGroup::idClicked), 
            this, &SettingsDialog::onLayoutChanged);
}

void SettingsDialog::loadSettings()
{
    m_settings = new QSettings("FloatingMenu", "Settings");
    int layout = m_settings->value("layout", CircularLayout).toInt();
    
    setCurrentLayout(static_cast<LayoutType>(layout));
}

void SettingsDialog::saveSettings()
{
    if (m_settings) {
        m_settings->setValue("layout", static_cast<int>(m_currentLayout));
        m_settings->sync();
    }
}

SettingsDialog::LayoutType SettingsDialog::getSelectedLayout() const
{
    return m_currentLayout;
}

void SettingsDialog::setCurrentLayout(LayoutType layout)
{
    m_currentLayout = layout;
    
    if (layout == CircularLayout) {
        m_circularRadio->setChecked(true);
    } else {
        m_scatteredRadio->setChecked(true);
    }
}

void SettingsDialog::onOkClicked()
{
    saveSettings();
    emit layoutChanged(m_currentLayout);
    accept();
}

void SettingsDialog::onCancelClicked()
{
    reject();
}

void SettingsDialog::onLayoutChanged()
{
    int selectedId = m_layoutButtonGroup->checkedId();
    if (selectedId >= 0) {
        m_currentLayout = static_cast<LayoutType>(selectedId);
        // 发出信号，让主窗口实时预览布局变化
        emit layoutChanged(m_currentLayout);
    }
}
